<?php
namespace frontend\controllers;

use Yii;
use yii\web\Session;
use yii\web\Controller;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
use backend\models\user\Admin;
use backend\models\user\Like;
use backend\models\course\Course;
use backend\models\user\TeacherInfo;
use backend\models\user\TeacherFrom;
use backend\models\user\TeacherPosition;


/*
*	模块：个人中心
*	作者：王文秀
*	时间：2015/12/10 
*/

class TeacherController extends Controller{
	
	public $pagination;
	public $teacher;
	public $position;
	public $from;
	//师资力量
	public function actionIndex(){
		$cond = ['and',['>','teacher_id','1'],['=','status','10']];
		$this -> actionPage($cond);
		//print_r($this ->teacher);die; 
		return $this->render('index',[
						'from' => $this -> from,
						'pagination' => $this ->pagination,
						'position' => $this -> position,
						'teacher' => $this ->teacher,
		]);
	}
	//分页
	public function actionPage($cond){
		//获取教师来源
		$teacher_from = new TeacherFrom;
		$this -> from = $teacher_from -> From();
		//获取教师职位
		$teacher_position = new TeacherPosition;
		$this -> position = $teacher_position -> Position();
		$teacher_info = new TeacherInfo;
		$this -> pagination = new Pagination([
			'defaultPageSize' => 10,
			'totalCount' => $teacher_info -> Count($cond),
		]);
		$data = $teacher_info -> Search($cond,$this -> pagination);
		//教师喜欢数
		$like = new Like;
		$session = Yii::$app->session;
		$user_id = $session->get('user_id');
		foreach($data as $key => $val){
			foreach($this -> from as $f){
				if($val['from_id'] == $f['from_id']){
					$data[$key]['from_name'] = $f['from_name'];
				}
			}
			foreach($this -> position as $p){
				if($val['position_id'] == $p['position_id']){
					$data[$key]['position_name'] = $p['position_name'];
				}
			}
			$is_like = $like -> Like($user_id,$val['teacher_id']);
			$data[$key]['is_like'] = $is_like ? 1 : 0;
			$like_count = $like -> LikeCount($val['teacher_id']);
			$data[$key]['like'] = $like_count ? $like_count : '0';
		}
		//按喜欢多少进行排序
		ArrayHelper::multisort($data,['recommend','like'], [SORT_DESC,SORT_DESC]);
		$this -> teacher = $data;
	}
	//搜索教师
	public function actionTeacher(){
		$teacher = Yii::$app->request->get();
		$from_id = $teacher['from_id'];
		$position_id = $teacher["position_id"];
		$cond = ['and',['>','teacher_id','1']];
		if(!empty($from_id)){
			$cond[] = ['=','from_id',$from_id];
		}
		if(!empty($position_id)){
			$cond[] = ['=','position_id',$position_id];
		}
		$this -> actionPage($cond);
		return $this->renderPartial('teacher',[
						'pagination' => $this ->pagination,
						'teacher' => $this ->teacher,
		]);
	}
	//教师详情
	public function actionTeacherinfo(){
		$teacher = Yii::$app->request->get();
		$teacher_id = $teacher['teacher_id'];
		$course = new Course;
		$cond = ['and',['=','status','1'],['=','teacher_id',$teacher_id]];
		$pagination = new Pagination([
			'defaultPageSize' => 9,
			'totalCount' => $course -> TeacherCourseCount($cond),
		]);
		$data = $course -> TeacherCourse($cond,$pagination);
		return $this->render('teacherinfo',[
						'course' => $data,
						'pagination' => $pagination,
		]);
	}
	//喜欢教师
	public function actionTeacherlike(){
        if (! Yii::$app->request->isAjax){
            return $this->goHome();
        } 
		$session = Yii::$app->session;
		$user_id = $session->get('user_id');
		$teacher = Yii::$app->request->get();
		$teacher_id = $teacher['teacher_id'];
		if($user_id){
			$like = new Like;
			$data = $like -> Like($user_id,$teacher_id);
			if($data){
				return json_encode([
					'code' => 2,
					'msg'  => '不能重复喜欢！',
				]);die;
			}else{
				$like -> user_id = $user_id;
				$like -> teacher_id = $teacher_id;
				$like -> like_addtime = time();
				$res = $like -> save();
				if($res){
					$like_count = $like -> LikeCount($teacher_id);
					return json_encode([
						'code' => 3,
						'msg'  => '喜欢成功',
						'data' => $like_count,
					]);die;
				}else{
					return json_encode([
						'code' => 4,
						'msg'  => '喜欢失败',
					]);die;
				}
			}
		}else{
			return json_encode([
					'code' => 1,
					'msg'  => '请先登录！',
				]);die;
		}
	}
	//教师详情公共
	public function CenterHeader(){
		$teacher = Yii::$app->request->get();
		$teacher_id = $teacher['teacher_id'];
		$teacher_from = new TeacherFrom;
		$from = $teacher_from -> From();
		//获取教师职位
		$teacher_position = new TeacherPosition;
		$position = $teacher_position -> Position();
		$teacher_info = new TeacherInfo;
		
		$data = $teacher_info -> TeacherInfo($teacher_id);
		//教师喜欢数
		$like = new Like;
		foreach($from as $f){
			if($data['from_id'] == $f['from_id']){
				$data['from_name'] = $f['from_name'];
			}
		}
		foreach($position as $p){
			if($data['position_id'] == $p['position_id']){
				$data['position_name'] = $p['position_name'];
			}
		}
		$like_count = $like -> LikeCount($teacher_id);
		$data['like'] = $like_count ? $like_count : '0';
		return $data;
	}
}